import { overdueCountTable, 
         overdueCountTrend,
         overdueCountPie,

         overdueCountTableForXBeauty, 
         overdueCountTrendForXBeauty,
         overdueCountPieForXBeauty,
       } from '../../services/channel/api';

export default {
  namespace: 'overdueStatistics',

  state: {
    loading: true,

    overdueTableData: {
      list: [],
      pagination: {},
    },

    overduePieData: {
      total: '',
      data: {},
    }
  },

  effects: {
    *fetchOverdueCountTable({ payload, callback }, { call, put }) {
      const response = yield call(overdueCountTable, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            overdueTableData: {
              list: response.data.list,
              pagination: {
                current: response.data.current,
                totalPage: response.data.totalPage,
                totalCount: response.data.totalCount,
              },
            } 
          },
        });
        if (callback) callback();
      }
    },

    *fetchOverdueCountTrend({ payload, callback }, { call, put }) {
      const response = yield call(overdueCountTrend, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { overdueTrendData: response.data },
        });
        if (callback) callback();
      }
    },

    *fetchOverdueCountPie({ payload, callback }, { call, put }) {
      const response = yield call(overdueCountPie, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            overduePieData: {
              total: response.data.totalAmount,
              data: response.data.stageMap,
            },
          },
        });
        if (callback) callback();
      }
    },

    //想美
    *fetchOverdueCountTableForXBeauty({ payload, callback }, { call, put }) {
      const response = yield call(overdueCountTableForXBeauty, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            overdueTableData: {
              list: response.data.list,
              pagination: {
                current: response.data.current,
                totalPage: response.data.totalPage,
                totalCount: response.data.totalCount,
              },
            } 
          },
        });
        if (callback) callback();
      }
    },

    *fetchOverdueCountTrendForXBeauty({ payload, callback }, { call, put }) {
      const response = yield call(overdueCountTrendForXBeauty, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { overdueTrendData: response.data },
        });
        if (callback) callback();
      }
    },

    *fetchOverdueCountPieForXBeauty({ payload, callback }, { call, put }) {
      const response = yield call(overdueCountPieForXBeauty, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            overduePieData: {
              total: response.data.totalAmount,
              data: response.data.stageMap,
            },
          },
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
